package org.xujin.book.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.xujin.book.po.Student;
/**
 * 
* @ClassName: Query
* @Description: TODO
* @author Xu,Jin Software_King@qq.com
* @Site:http://www.xujin.org
 */
public class Query {
	public static void main(String[] args) { 
		/*创建一个Configuration()的实例，调用configure()方法加载hibernate.cfg.xml配置文件
		 * 如果hibernate.cfg.xml没有放在默认的位置:classpath下
		 * 举例：Configuration conf = new Configuration().configure("/hibernate.cfg.xml配置文件的路径/hibernate.cfg.xml");
		 * 
		 * */
		Configuration conf = new Configuration().configure();
		/*conf调用buildSessionFactory()方法创建SessionFactory,相当于创建数据源*/
		SessionFactory sf = conf.buildSessionFactory();
		/*打开会话*/
		Session session = sf.openSession();
		/*利用session查询数据的方法get(PO对应的类，数据库中表的主键)*/
		Student stu = (Student)session.get(Student.class, "2010023001");
		/*用学生对象调用get()方法获取属性，然后输出*/
		String stuno = stu.getStuno();
		String stuname = stu.getName();
		int stuage = stu.getAge();
		System.out.println("学号:" + stuno);
		System.out.println("姓名:" + stuname);
		System.out.println("年龄:" + stuage);
		//关闭session
		session.close();
	}
}